package com.grandbroad.eim.base;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.grandbroad.eim.utils.CacheUtils;

import java.util.Map;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.event.LoginStateChangeEvent;
import cn.jpush.im.android.api.model.UserInfo;
import cn.jpush.im.api.BasicCallback;

/**
 *activity的共有功能
 * Created by even alone on 2018/6/2.
 */

public class LibActivity extends AppCompatActivity
{
    private ProgressDialog dialog;
    private AlertDialog.Builder builder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //推送相关
        JMessageClient.setNotificationFlag(JMessageClient.FLAG_NOTIFY_DEFAULT);
        //目的是为了监听登录状态
        JMessageClient.registerEventReceiver(this);

    }

    /**
     * 登录状态发送变化的事件
     * @param event 事件
     */
    public void onEventMainThread(LoginStateChangeEvent event){
        switch (event.getReason())
        {
            case user_deleted:
                //用户被删除
                com.grandbroad.eim.utils.ToastUtils.show("账户被删除");
                break;
            case user_logout:
                //用户退出
                break;
            case user_password_change:
                //用户密码被改
                break;
            case user_disabled:
                //用户被禁止登陆
                com.grandbroad.eim.utils.ToastUtils.show("禁止登陆");
                CacheUtils.rmoveUser();
                break;
            case user_login_status_unexpected:
                //用户登陆被挤了
                com.grandbroad.eim.utils.ToastUtils.show("该账号在其他地方被登陆");
                CacheUtils.rmoveUser();
                break;
        }
    }

    public void showSelectDialog(String title, String message, DialogInterface.OnClickListener successlistener, DialogInterface.OnClickListener faildlistener)
    {
        AlertDialog.Builder al = new AlertDialog.Builder(this);
        al.setMessage(message);
        al.setTitle(title);
        al.setCancelable(false);
        al.setPositiveButton("确定",successlistener);
        al.setNegativeButton("取消",faildlistener);
        al.show();
    }
    /**
     * 打开进度框
     * @param msg
     */
    protected void showProgessDialog(String msg)
    {
        if (!TextUtils.isEmpty(msg))
        {
            dialog = new ProgressDialog(this);
            dialog.setMessage(msg);
            dialog.setCancelable(false);
            dialog.show();
        }
    }
    /**
     * 打开进度框
     * @param msg
     */
    protected void showProgessDialog(String msg,boolean flag)
    {
        if (!TextUtils.isEmpty(msg))
        {
            dialog = new ProgressDialog(this);
            dialog.setMessage(msg);
            dialog.setCancelable(flag);
            dialog.show();
        }
    }

    /**
     * 延时关闭进度条+跳转
     * @param msg
     * @param mills
     */
    protected void showProgressDialog(final String msg, long mills, final Class clazz, final boolean flag)
    {

        dialog = new ProgressDialog(LibActivity.this);
        dialog.setMessage(msg);
        dialog.setCancelable(false);
        dialog.show();
        //关闭
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dialog.hide();
                startActivity(clazz,flag);
            }
        },mills);

    }
    /**
     * 延时跳转
     * @param mills
     */
    protected void startActivityDelay(long mills, final Class clazz, final boolean flag)
    {
        if (flag)
        {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    startActivity(new Intent(LibActivity.this,clazz));
                    finish();
                }
            },mills);
        }
        else
        {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    startActivity(new Intent(LibActivity.this,clazz));
                }
            },mills);
        }

    }
    /**
     * 打开普通对话框
     * @param title 标题
     * @param msg 内容
     * @param flag 是否强制关闭
     * @param onClickListener 监听
     */
    protected void showNormalDialog(boolean flag,String title, String msg,String cancle, DialogInterface.OnClickListener onClickListener)
    {
        builder = new AlertDialog.Builder(LibActivity.this);
        builder.setTitle(title);
        builder.setCancelable(flag);
        builder.setMessage(msg);
        builder.setPositiveButton("确定",onClickListener);
        builder.setNegativeButton(cancle, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //空实现达到关闭对话框的目的
            }
        });
        builder.show();
    }
    /**
     * 关闭
     */
    protected void dissmissDialog()
    {
        if (dialog!=null)
        {
            dialog.hide();
        }
    }
    /**
     * 跳转页面
     * @param clazz 页面
     * @param flag 条件
     */
    protected void startActivity(Class clazz,boolean flag)
    {
        if (flag)
        {
            startActivity(new Intent(this,clazz));
            finish();
            return;
        }
        startActivity(new Intent(this,clazz));
    }

    /**
     * 带数据跳转
     * @param clazz
     * @param flag
     * @param bundle
     */
    protected void startActivityWithBundle(Class clazz, boolean flag, Bundle bundle)
    {
        Intent intent = new Intent(this,clazz);
        intent.putExtras(bundle);
        if (flag)
        {
            startActivity(intent);
            finish();
            return;
        }
        startActivity(intent);
    }

    /***
     * 带数据
     * @param clazz
     * @param flag
     * @param params
     */
    protected void startActivityWithParams(Class clazz, boolean flag, Map<String,String> params)
    {
        Intent intent = new Intent(this,clazz);
        for (String key:params.keySet())
        {
            intent.putExtra(key,params.get(key));
        }
        if (flag)
        {
            startActivity(intent);
            finish();
            return;
        }
        startActivity(intent);
    }

}